<template>
<!-- 文件管理新增按钮弹窗 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
		  <p-col  :span="24">
			<p-form-item label="文件编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'fileCode', validatorRules.fileCode]" placeholder="请输入文件编码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="文件名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'fileName', validatorRules.fileName]" placeholder="请输入文件名称"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="文件版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  disabled v-decorator="[ 'fileVersion', validatorRules.fileVersion]" placeholder="请输入文件版本"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="文件附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-upload v-decorator="['fileUrl', validatorRules.fileUrl]" :trigger-change="true" :ismultiSelect="false"></j-upload>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="文件类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['fileType', validatorRules.fileType]" :trigger-change="true" dictCode="quality_file_type" placeholder="请选择文件类型"/>
			</p-form-item>
		  </p-col>
        </p-row>
		  <p-row>
			  <p-col  :span="24">
				  <p-form-item label="修改说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
					  <p-textarea    v-decorator="[ 'remarks', validatorRules.remarks]" placeholder="请输入修改说明"></p-textarea >
				  </p-form-item>
			  </p-col>
		  </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>


  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { getAction,httpAction, putAction, api } from '@/api/manage'
  import pick from 'lodash.pick'
  import JUpload from '@/components/jeecg/JUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesQcQualityFileManagementModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JUpload,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
		  approvalList:[],

        validatorRules:{
        fileCode:{rules: [{ required: true, message: '请输入文件编码!' }]},
        fileName:{rules: [{ required: true, message: '请输入文件名称!' }]},
        fileVersion:{rules: [{ required: true, message: '请输入文件版本!' }]},
        fileUrl:{rules: [{ required: true, message: '请上传文件附件!' }]},
        fileType:{rules: [{ required: true, message: '请输入文件类型!' }]},
			remarks:{rules: [{ required: true, message: '请输入修改说明!' }]},
        },
        url: {
          add: '/mes-quality/mesQcQualityFileManagement/add',
          edit: '/mes-quality/mesQcQualityFileManagement/edit',
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.getRuleCodeValue();
      },
      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "mes_quality_file_rule", "columnName": "fileCode"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            item.fileVersion = 'V1.0';
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'fileCode','fileName','fileVersion','fileUrl','fileType','createBy','createTime','updateBy','updateTime','remarks','status'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
		handleOk () {
			const that = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
			  if (!err) {
				  that.confirmLoading = true;
				  let httpurl = '';
				  let method = '';
				  let formData = Object.assign(this.model, values);
				  this.formData = Object.assign(this.model, values);
				  //新增触发
				  if(!this.model.id){
					httpurl+=this.url.add;
					method = 'post';
					if(this.EnhanceJS.afterAdd){
					  //新增之前
					  let Obj = eval(this.EnhanceJS.afterAdd)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }else{
					//编辑触发
					httpurl+=this.url.edit;
					method = 'put';
					if(this.EnhanceJS.afterEdit){
					 //编辑之前
					  let Obj = eval(this.EnhanceJS.afterEdit)
					  new Obj(this)
					}else{

						if(formData.processId){
							this.getApprovalList(formData.processId,httpurl,formData,method);

						}else{
							that.modalFrom(httpurl,formData,method)
						}
					}
				  }
			  }

			})
		},
      handleCancel () {
        this.close()
      },

		getApprovalList(processInstanceId,httpurl,formData,method){
			const that = this;
			getAction('/poros-bpmengine/task/getTaskRecordList?processInstanceId='+processInstanceId).then(res=>{
				if(res.success){
					this.approvalList = res.data;
					let key  = 1;
					if(this.approvalList[0]&&this.approvalList[0].taskId){
						formData.taskId = this.approvalList[0].taskId;
					}

					that.modalFrom(httpurl,formData,method)
				}else{
					this.$message.warning(res.msg);
				}
			})
		},
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'fileCode','fileName','fileVersion','fileUrl','fileType','createBy','createTime','updateBy','updateTime','remarks','status'))
      },

      
    }
  }
</script>
